import 'dart:io';

import 'package:city_pickers/city_pickers.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:part_time_job_pro/common/base/base.dart';

import 'state.dart';

class ProxyLogic extends BaseController {
  final ProxyState state = ProxyState();

  ///输入监听
  late TextEditingController mobileTextEditController;
  late TextEditingController companyTextEditController;
  late TextEditingController contactTextEditController;
  late TextEditingController offerCountTextEditController;
  late TextEditingController jobSalaryTextEditController;
  late TextEditingController jobDescTextEditController;

  ///监听是否正在输入数据
  late FocusNode mobileFocusNode;
  late FocusNode companyFocusNode;
  late FocusNode contactFocusNode;
  late FocusNode offerCountFocusNode;
  late FocusNode jobSalaryFocusNode;
  late FocusNode jobDescFocusNode;

  //初始化
  @override
  void onInit() {
    super.onInit();
    companyTextEditController = TextEditingController();
    contactTextEditController = TextEditingController();
    mobileTextEditController = TextEditingController();
    offerCountTextEditController = TextEditingController();
    jobSalaryTextEditController = TextEditingController();
    jobDescTextEditController = TextEditingController();

    companyFocusNode = FocusNode();
    contactFocusNode = FocusNode();
    mobileFocusNode = FocusNode();
    offerCountFocusNode = FocusNode();
    jobSalaryFocusNode = FocusNode();
    jobDescFocusNode = FocusNode();
  }

  //注销函数
  @override
  void onClose() {
    contactTextEditController.dispose();
    companyTextEditController.dispose();
    mobileTextEditController.dispose();
    offerCountTextEditController.dispose();
    jobSalaryTextEditController.dispose();
    super.onClose();
  }

  ///监听用户名称输入
  updateCompanyName(String text) {
    state.companyName = text;
    update();
  }

  //清空账号输入
  clearCompanyName() {
    state.companyName = '';
    companyTextEditController.clear();
    companyFocusNode.unfocus();
    update();
  }

  ///监听联系人输入
  updateContactName(String text) {
    state.contactName = text;
    update();
  }

  //清空账号输入
  clearContactName() {
    state.contactName = '';
    contactTextEditController.clear();
    contactFocusNode.unfocus();
    update();
  }

  ///监听电话输入
  updateTel(String text) {
    state.mobileText = text;
    update();
  }

  //清空电话输入
  clearTel() {
    state.mobileText = '';
    mobileTextEditController.clear();
    mobileFocusNode.unfocus();
    update();
  }

  ///监听人数输入
  updateOfferCount(String text) {
    state.offerCount = text;
    update();
  }

  //清空人数输入
  clearOfferCount() {
    state.offerCount = '';
    offerCountTextEditController.clear();
    offerCountFocusNode.unfocus();
    update();
  }

  ///监听薪资输入
  updateJobSalary(String text) {
    state.jobSalary = text;
    update();
  }

  //清空薪资输入
  clearJobSalary() {
    state.jobSalary = '';
    jobSalaryTextEditController.clear();
    jobSalaryFocusNode.unfocus();
    update();
  }

  ///监听招聘要求输入
  updateJobDesc(String text) {
    state.jobDesc = text;
    update();
  }

  //清空招要求输入
  clearJobDesc() {
    state.jobDesc = '';
    jobDescTextEditController.clear();
    jobDescFocusNode.unfocus();
    update();
  }

  //选择地区
  showCitySelector(context) async {
    Result? result2;
    if (Platform.isAndroid) {
      result2 = await CityPickers.showFullPageCityPicker(
          context: context, locationCode: state.locationCode);
    } else {
      result2 = await CityPickers.showCityPicker(
          context: context, locationCode: state.locationCode);
    }

    if (result2 != null) {
      state.countryText =
          "${result2.provinceName} ${result2.cityName} ${result2.areaName}";
      state.locationCode =
          result2.areaId == null ? result2.cityId! : result2.areaId!;
    } else {
      state.countryText = "";
      state.locationCode = "";
    }
    update();
    LogUtil.e(result2);
  }

  //提交代理招聘
  submitOfferProxy() {}
}
